package org.paradise.dms.util;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import message.sms.SUBMIT;

import org.apache.log4j.Logger;
import org.paradise.dms.dao.impl.MessageOutDaoImpl;
import org.paradise.dms.dao.impl.MessageOutTwoDaoImpl;
import org.paradise.dms.pojo.MessageOut;

import common.ComFun;

@SuppressWarnings("unchecked")
class SubmitSms1 extends Thread {
	private static Logger logger = Logger.getLogger(SubmitSms1.class);
	
	
	
	private  MessageOut message;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
	public String telphone;
	public String content;
    
	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	

	public void run() {
		System.out.println("******run");
		String [] ss = this.getTelphone().split(",");
        String con = this.getContent();
		for (int i = 0; i < ss.length; i++) {
			try {
				Thread.sleep(1000);
				SUBMIT mt = new SUBMIT();			
				mt.setSrcID("106573001972");
				mt.setDestID(ss[i]); // 用户号码
				mt.setServiceID("");//下行通道。（或下行短信业务）
				mt.setMsgContent(con);// 短信内容��������
				
				String retVal = "1";
				if ("1".equals(retVal) || "2".equals(retVal)
						|| "3".equals(retVal)) {
					Thread.sleep(1000);
					System.out.println(mt);
					retVal = ComFun.sendSMS(mt);					
					if ("1".equals(retVal)) {						
						logger.error("the SubmitPool was full.");
					} else if ("2".equals(retVal)) {
						logger.error("没有建立连接");
					} else if ("3".equals(retVal)) {
						logger.error("流量控制错误");
					}else{
						logger.info("企业下行消息标识 ["+retVal+"]，"
								+"源号码 ["+mt.getSrcID()+"]，"
								+"目的号码 ["+mt.getDestID()+"]，"
								+"短信内容 ["+mt.getMsgContent()+"]");
						MessageOut message = new MessageOut();
						message.setMessagecontent(mt.getMsgContent());
						message.setReceivertelephone(mt.getDestID());
						message.setMessagesend(retVal);
						Date date = new Date(); 
						Timestamp nousedate = new Timestamp(date.getTime()); 
						message.setMessagetime(nousedate);
						MessageOutTwoDaoImpl messageOutDaoImpl = new MessageOutTwoDaoImpl();	
						messageOutDaoImpl.add(message);	
					}
				}						
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(this, e);
			}
		}
	System.out.println("----------------------");
	}


	public MessageOut getMessage() {
		return message;
	}

	public void setMessage(MessageOut message) {
		this.message = message;
	}

}			